一覧に戻る

Convert zxy tileindex to lnglat bounds in Rust

#rust#geospatial

A func to convert z/x/y tile index to lnglat bounds.

fn mercator_zxy_to_lonlat_bounds(z: u32, x: u32, y: u32) -> (f64, f64, f64, f64) {
	let n = 2_f64.powi(z as i32);
	
	// Calculate longitude bounds
	let lon_min = x as f64 / n * 360.0 - 180.0;
	let lon_max = (x + 1) as f64 / n * 360.0 - 180.0;
	
	// Calculate latitude bounds using Web Mercator projection formula
	let lat_max = (std::f64::consts::PI * (1.0 - 2.0 * y as f64 / n))
		.sinh()
		.atan()
		.to_degrees();
	
	let lat_min = (std::f64::consts::PI * (1.0 - 2.0 * (y + 1) as f64 / n))
		.sinh()
		.atan()
		.to_degrees();
		(lon_min, lat_min, lon_max, lat_max)
}